# File    :   docker-run-oracle-latest.sh
# Time    :   2023/06/09 10:31:34
# Author  :   Liu JC
# Version :   1.0
# Contact :   liujc.255@qq.com
# Desc    :   None


Help()
{
   # Display Help
   echo "Pull container-registry.oracle.com/database/free:latest image and run container in docker."
   echo
   echo "Syntax: scriptTemplate [-h|p|n] ..."
   echo "options:"
   echo "-h, --help     Print this Help."
   echo "-p, --port     Publish or expose port."
   echo "-n, --name     Container name."
   echo "-P, --path     Mount volume."
   echo "-s, --pwd      ROOT passwd."
   echo
}

EmptyParameter()
{
    # empty parameter after arguments
    echo "Empty parameter."
}

# defalut setting
port=11521
name=oracle
path=/usr/local/docker
pwd=Oracle.123

POSITIONAL=()
while (( $# > 0 )); do
    case "${1}" in
        -p|--port)
        if [[ -n "${2}" ]]; then
            port=${2}
        else
            EmptyParameter
        fi        
        shift 2 # shift once since flags have no values
        ;;
        -n|--name)
        if [[ -n "${2}" ]]; then
            name=${2}
        else
            EmptyParameter
        fi        
        shift 2 
        ;;
        -P|--path)
        if [[ -n "${2}" ]]; then
            path=${2}
        else
            EmptyParameter
        fi
        shift 2
        ;;
        -s|--pwd)
        if [[ -n "${2}" ]]; then
            pwd=${2}
        else
            EmptyParameter
        fi
        shift 2
        ;;
        -h|--help)
        Help
        exit 0
        ;;
        *) # unknown flag/switch
        POSITIONAL+=("${1}")
        shift      
        ;;
    esac
done

path=${path}/${name}

set -- "${POSITIONAL[@]}" # restore positional params


if [ ! -d "${path}" ]; then
    sudo mkdir "${path}"
    # it will crash if the directory has no write permission
    sudo chmod -R o+w ${path}
fi

# sudo docker rm ${name}
sudo docker run -p ${port}:1521 --name ${name}\
 -e ORACLE_PWD=${pwd}\
 -v ${path}/oracledata:/opt/oracle/oradata\
 container-registry.oracle.com/database/free:latest

